package com.cloudsigns.factorymanager2.project.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.EditDispatchInformationForEngineeringInstallationOrderActivity;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.project.iview.IGetProduceOrderDetailView;
import com.cloudsigns.factorymanager2.project.iview.IProductOrderDeleteView;
import com.cloudsigns.factorymanager2.project.iview.IProductOrderSendView;
import com.cloudsigns.factorymanager2.project.presenter.GetProduceOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.presenter.ProductOrderDeletePresenter;
import com.cloudsigns.factorymanager2.project.presenter.ProductOrderSendPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

public class ProductionTaskOrderListItemActivity extends BaseActivity implements IGetProduceOrderDetailView, IProductOrderSendView, IProductOrderDeleteView {
    private ImageView img_back;
    private TextView tv_title;
    private RelativeLayout rl_order_information;
    private TextView tv_project_name, tv_engineering_name;
    private ImageView img_dispatch_information;
    private LinearLayout ll_dispatch_information;
    private RelativeLayout rl_company_name;
    private TextView edt_dispatch_way, edt_company_name, edt_receive_people;
    private TextView btn_dispatch_order, btn_delete_order;

    private GetProduceOrderDetailPresenter detailPresenter;
    private UserInfoBean userInfoBean;
    private ProduceProduceOrderBean produceOrderBean;
    private boolean isDelete = true, isDispatch;

    private ProductOrderSendPresenter productOrderSendPresenter;
    private ProductOrderDeletePresenter productOrderDeletePresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_product_task_order_list_item);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        rl_order_information = findViewById(R.id.rl_order_information);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        img_dispatch_information = findViewById(R.id.img_dispatch_information);
        ll_dispatch_information = findViewById(R.id.ll_dispatch_information);
        rl_company_name = findViewById(R.id.rl_company_name);
        edt_dispatch_way = findViewById(R.id.edt_dispatch_way);
        edt_company_name = findViewById(R.id.edt_company_name);
        edt_receive_people = findViewById(R.id.edt_receive_people);
        btn_dispatch_order = findViewById(R.id.btn_dispatch_order);
        btn_delete_order = findViewById(R.id.btn_delete_order);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText("生产任务单");
        detailPresenter = new GetProduceOrderDetailPresenter(this);

        Intent intent = getIntent();
        if (null != intent) {
            produceOrderBean = (ProduceProduceOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        if (null != produceOrderBean) {
            if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.CREATED) {
                ll_dispatch_information.setVisibility(View.GONE);
                img_dispatch_information.setVisibility(View.VISIBLE);
            } else if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.SEND) {
                ll_dispatch_information.setVisibility(View.VISIBLE);
                img_dispatch_information.setVisibility(View.VISIBLE);
            } else if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.RECEIVED) {
                ll_dispatch_information.setVisibility(View.VISIBLE);
                img_dispatch_information.setVisibility(View.GONE);
            } else if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.RETURNED) {
                ll_dispatch_information.setVisibility(View.VISIBLE);
                img_dispatch_information.setVisibility(View.GONE);
            } else if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.COMPLETED) {
                ll_dispatch_information.setVisibility(View.VISIBLE);
                img_dispatch_information.setVisibility(View.GONE);
            } else {
                img_dispatch_information.setVisibility(View.GONE);

                isDispatch = false;
            }
            setClickable();
        }

        tv_project_name.setText(produceOrderBean.getProjectName());
        tv_engineering_name.setText(" / " + produceOrderBean.getEngineeringName());

        detailPresenter.getProduceOrderDetail();

        productOrderSendPresenter = new ProductOrderSendPresenter(this);
        productOrderDeletePresenter = new ProductOrderDeletePresenter(this);

    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(this);
        rl_order_information.setOnClickListener(this);
        img_dispatch_information.setOnClickListener(this);
        btn_dispatch_order.setOnClickListener(this);
        btn_delete_order.setOnClickListener(this);
    }

    private void setClickable() {
        if (isDispatch)
            btn_dispatch_order.setBackgroundColor(getResources().getColor(R.color.rgb_00B050));
        else
            btn_dispatch_order.setBackgroundColor(getResources().getColor(R.color.rgb_D9D9D9));

        if (isDelete)
            btn_delete_order.setBackgroundColor(getResources().getColor(R.color.rgb_C00000));
        else
            btn_delete_order.setBackgroundColor(getResources().getColor(R.color.rgb_D9D9D9));
    }

    private void initProduceOrderDispatchInfo() {
        if (null != produceOrderBean) {
            edt_dispatch_way.setText("内派");
            edt_receive_people.setText(produceOrderBean.getReceiveAccountName() + ":" + produceOrderBean.getReceiveAccountId());
            rl_company_name.setVisibility(View.GONE);
        }
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 返回
            case R.id.img_back:
                finish();
                break;
            // 工单详情
            case R.id.rl_order_information:
                mIntent = new Intent(ProductionTaskOrderListItemActivity.this, ProduceOrderDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, produceOrderBean);
                startActivity(mIntent);
                break;
            // 派工信息
            case R.id.img_dispatch_information:
                if (null != produceOrderBean) {
                    if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.CREATED || produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.RETURNED || produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.SEND) {
                        mIntent = new Intent(ProductionTaskOrderListItemActivity.this, EditDispatchInformationForEngineeringInstallationOrderActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, CommonEnum.ROLECATEGORY.MANAGER);
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                }
                break;
            // 派发工单
            case R.id.btn_dispatch_order:
                if (isDispatch) {
                    isDispatch = false;
                    setClickable();
                    productOrderSendPresenter.productOrderSend();
                }
                break;
            // 删除工单
            case R.id.btn_delete_order:
                if (isDelete) {
                    isDelete = false;
                    setClickable();
                    productOrderDeletePresenter.productOrderDelete();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    String dispatch_way = data.getStringExtra("dispatch_way");
                    edt_dispatch_way.setText(dispatch_way);
                    if (dispatch_way.equals("内派")) {
                        userInfoBean = (UserInfoBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                        if (null != userInfoBean)
                            edt_receive_people.setText(userInfoBean.getName() + ":" + userInfoBean.getId());
                        rl_company_name.setVisibility(View.GONE);
                    } else if (dispatch_way.equals("外派")) {
                        edt_receive_people.setText(data.getStringExtra(ConstantUtil.KEY_1));
                        edt_company_name.setText(data.getStringExtra(ConstantUtil.KEY_2));
                        rl_company_name.setVisibility(View.VISIBLE);
                    }
                    ll_dispatch_information.setVisibility(View.VISIBLE);
                    isDelete = true;
                    isDispatch = true;
                    setClickable();
                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getOrderId() {
        if (null != produceOrderBean)
            return produceOrderBean.getOrderId();
        return null;
    }

    @Override
    public void deleteSunccessed() {
        showToast(getResources().getString(R.string.delete_success));
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void deleteFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        if (produceOrderBean.getStatus() == CommonEnum.ProduceOrderStatus.CREATED) {
            isDelete = true;
            isDispatch = true;
        } else {
            isDispatch = false;
            isDelete = true;
        }
        setClickable();
    }

    @Override
    public String getReceiveAccountId() {
        if (null != userInfoBean)
            return userInfoBean.getId();
        return "";
    }

    @Override
    public void sendSuccess() {
        showToast(getResources().getString(R.string.dispatch_success));
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void sendFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        isDelete = true;
        isDispatch = true;
        setClickable();
    }

    @Override
    public void getProduceOrderDetailSuccessed(ProduceProduceOrderBean produceOrderBean) {
        this.produceOrderBean = produceOrderBean;
        initProduceOrderDispatchInfo();
    }

    @Override
    public void getProduceOrderDetailFailed(String desc) {
        Toast.makeText(this, "获取详情失败" + desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
